#include <QtTest/QtTest>
#include "../fileParser.h"
#include <cmath>
#include "../Vector3D.h"

class UnitTests: public QObject
{
    Q_OBJECT
private slots:
    void init();
    void cleanup();
    void firstTest();
    void secondTest();
    void fileParserTest3();
    void vector3Dtest1();
    void vector3Dtest2();
    void vector3Dtest3();
private:
    fileParser* parser;
    //QVector<QString> testFileNames;
    bool approximate(float firstValue, float secondValue, float delta=1e-12);
};

void UnitTests::init()
{
    parser = new fileParser();
    //testFileNames << ":/data/test1.dat" <<
    //        ":/test2.dat";
}

void UnitTests::cleanup()
{
    delete parser;
}

void UnitTests::firstTest()
{
    parser->parse(":/data/test1.dat");
    QVERIFY(parser->getMin() == 0 && parser->getMax() == 1);
}

void UnitTests::secondTest()
{
    parser->parse(":/data/test2.dat");
    QVERIFY(approximate(parser->getMin(), -123.7) &&
            approximate(parser->getMax(), 12.7789e3));
}

void UnitTests::fileParserTest3()
{
    try {
        parser->parse(":/data/test3.dat");
        QFAIL("Expecting fileParser::BadFileStructureEx exception when parsing test3.dat.");
    } catch (fileParser::BadFileStructureEx ex) {
        //PASSED
    } catch (...){
        //QFAIL("Expecting fileParser::BadFileStructureEx exception when parsing test3.dat.");
        QFAIL("Wrong exception thrown when parsing test3.dat");
    }
}

void UnitTests::vector3Dtest1()
{
    //testing default constructor
    Vector3D vector;
    QVERIFY(vector.x()==0 && vector.y()==0 && vector.z()==0);
}

void UnitTests::vector3Dtest2()
{
    //testing constructor initializing values from three numbers
    Vector3D vector(3, 8, 17.765);
    QVERIFY(vector.x()==3 && vector.y()==8 && approximate(vector.z(),17.765));
}

void UnitTests::vector3Dtest3()
{
    //testing constructor initializing values from QPointF and one number
    QPointF vector2d(3, 8);
    Vector3D vector3d(vector2d, 17.765);
    QVERIFY(vector3d.x()==3 && vector3d.y()==8 && approximate(vector3d.z(),17.765));
}

bool UnitTests::approximate(float firstValue, float secondValue, float delta)
{
    return fabs(firstValue-secondValue) < delta;
}

QTEST_MAIN(UnitTests)
#include "unittests.moc"

